package lock

import (
	"YingShop/log"
	"fmt"

	"github.com/go-redsync/redsync/v4"
)

type baseLock struct {
	ItemId int
	mutex  *redsync.Mutex
}

func (baseLock *baseLock) init(lockName string) {
	if baseLock.mutex == nil {
		baseLock.mutex = redisLockClient.NewMutex(lockName)
	}
}

func (baseLock *baseLock) Lock() error {
	err := baseLock.mutex.Lock()
	if err != nil {
		log.Error(err.Error())
	}
	return err
}

func (baseLock *baseLock) UnLock() (bool, error) {
	ok, err := baseLock.mutex.Unlock()
	log.Info(fmt.Sprintf("解锁%d状态:%v", baseLock.ItemId, ok))
	if err != nil {
		log.Error(err.Error())
	}
	return ok, err
}

func (baseLock baseLock) GetLockName() string {
	panic("please overwrite GetLockName")
}
